<template>
  <div ref="chartRef" :style="{ height, width }"></div>
</template>
<script lang="ts">
  import { defineComponent, PropType, ref, Ref, reactive, watchEffect } from 'vue';
  import { useECharts } from '/@/hooks/web/useECharts';
  import { cloneDeep } from 'lodash-es';
  export default defineComponent({
    name: 'Analysis',
    props: {
      chartData: {
        type: Array,
        default: () => [],
      },
      option: {
        type: Object,
        default: () => ({}),
      },
      width: {
        type: String as PropType<string>,
        default: '100%',
      },
      height: {
        type: String as PropType<string>,
        default: 'calc(100vh - 78px)',
      },
    },
    setup(props) {
      const chartRef = ref<HTMLDivElement | null>(null);
      const { setOptions, echarts } = useECharts(chartRef as Ref<HTMLDivElement>);
      const option = reactive({
        // title:{
        //   text:'缺陷数量总和/个'
        // },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow',
          },
        },
        legend: {
          textStyle: {
            color: '#FFFFFF',
          },
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: [],
          axisTick: {
            show: false,
          },
          axisLabel: {
            textStyle: {
              color: '#FFFFFF'
            },
          },
        },
        yAxis: {
          type: 'value',
          splitNumber: 4,
          axisTick: {
            show: true,
          },
          axisLabel: {
            textStyle: {
              color: '#FFFFFF'
            },
          },
        },
        grid: { left: '1%', right: '1%', top: '2  %', bottom: 0, containLabel: true },
        series: [
          {
            type: 'line',
            data: [],
          },
        ],
      });

      watchEffect(() => {
        props.chartData && initCharts();
      });

      function initCharts() {
        if (props.option) {
          Object.assign(option, cloneDeep(props.option));
        }

        const times = props.chartData.map((item) => item.time);
        const count = props.chartData.map((item) => item.count ? item.count:0);
        let tagData = {
          smooth: true,
          data: count,
          type: 'line',
          symbol: 'none',
          lineStyle: {
            color: '#34CB6E',
            width: 4,
            shadowColor: 'rgba(0,0,0,1)',
            shadowBlur: 8,
            shadowOffsetY: 8,
          },
          label: {
            show: false,
          },
          areaStyle: {
            color: 'rgba(25, 255, 101, 0.2)',
          },
        };
        let seriesData = [];
        seriesData.push(tagData);
        option.series = seriesData;
        option.xAxis.data = times;
        setOptions(option);
      }

      return { chartRef };
    },
  });
</script>
